package com.yayan.beautyclub.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yayan.beautyclub.bean.Product;
import com.yayan.beautyclub.dao.IProductDao;
import com.yayan.beautyclub.dao.ProductDao;

@Service
public class ProductService implements IProductService{

	private IProductDao productDao;
	
	public ProductService(){
		productDao = new ProductDao();
	}
	
	@Override
	public Map<String, Object> findProductBypId(long pId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = null;
		try{
			if(pId != 0L)
				map = productDao.findProductBypId(pId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public List<Product> findProductBycId(int cId, int page, int size) {
		// TODO Auto-generated method stub
		List<Product> list = null;
		try{
			if(cId != 0 && page > 0 && size > 0)
				list = productDao.findProductBycId(cId, page, size);
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public List<Product> findProductByParentId(int parentId, int page, int size) {
		// TODO Auto-generated method stub
		List<Product> list = null;
		try{
			if(parentId != 0 && page > 0 && size > 0)
				list = productDao.findProductByParentId(parentId, page, size);
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public long saveProduct(Product product) {
		// TODO Auto-generated method stub
		long pId = 0;
		try{
			if(product.getcId() != 0 && product.getpName() != null && !product.getpName().isEmpty()){
				if(product.getTime() == null){
					product.setTime(new Date());
				}
				pId = productDao.save(product);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return pId;
	}

	@Override
	public boolean updateProduct(Product product) {
		// TODO Auto-generated method stub
		boolean res = false;
		try{
			if(product.getpId() != 0L && product.getcId() != 0 &&
				product.getpName() != null && !product.getpName().isEmpty()){
				if(product.getTime() == null){
					product.setTime(new Date());
				}
				res = productDao.update(product);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public boolean deleteProduct(long pId) {
		// TODO Auto-generated method stub
		boolean res = false;
		try{
			if(pId != 0L){
				res = productDao.delete(pId);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public boolean addProductSales(long pId) {
		// TODO Auto-generated method stub
		boolean res = false;
		try{
			if(pId != 0L){
				res = productDao.addSales(pId);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public List<Map<String, Object>> findProductByCondition(int page, int size, int firstcId, int secondcId,
			String searchKey) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = null;
		try{
			if(page > 0 && size > 0){
				list = productDao.findProductByCondition(page, size, firstcId, secondcId, searchKey);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Product> findProductByActivity() {
		// TODO Auto-generated method stub
		List<Product> list = null;
		try{
			list = productDao.findProductByActivity();
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

}
